{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anaconda\\lib\\site-packages\\pandas\\compat\\_optional.py:138: UserWarning: Pandas requires version '2.7.0' or newer of 'numexpr' (version '2.6.9' currently installed).\n",
      "  warnings.warn(msg, UserWarning)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 概述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# DataFrame是一个由两个维度标签标记的数据结构，每列数据可以是不同的类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程\n",
       "0    坎坷  72  81  59  80  55\n",
       "1  温柔岁月  79  38  64  46  95\n",
       "2  时空倒流  69  31  93  49  70\n",
       "3    沐白  55  89  75  85  56\n",
       "4   苏牧待  85  66  80  47  67\n",
       "5  山河之志  54  39  75  42  69\n",
       "6    萤火  40  39  90  71  93\n",
       "7    烟火  81  75  75  56  94"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 主要有两种方式：\n",
    "# 1. pd.DataFrame(np.random.randint(40,100,size=(5,2)),index=[...],columns=[..])\n",
    "# 2. Series字典，下面的例子就是用这个方式\n",
    "names = [\"坎坷\",\"温柔岁月\",\"时空倒流\",\"沐白\",\"苏牧待\",\"山河之志\",\"萤火\",\"烟火\"]\n",
    "num = len(names)\n",
    "data = pd.DataFrame({\"name\":names,\"语文\":np.random.randint(40,90,size=num),\n",
    "                    \"数学\":np.random.randint(30,99,size=num),\"外文\":np.random.randint(50,100,size=num),\n",
    "                    \"物理\":np.random.randint(40,100,size=num),\"编程\":np.random.randint(50,98,size=num)})\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    72\n",
       "1    79\n",
       "2    69\n",
       "3    55\n",
       "4    85\n",
       "5    54\n",
       "6    40\n",
       "7    81\n",
       "Name: 语文, dtype: int32"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选择一个列\n",
    "data[\"语文\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  生物\n",
       "0    坎坷  72  81  59  80  55   0\n",
       "1  温柔岁月  79  38  64  46  95   0\n",
       "2  时空倒流  69  31  93  49  70   0\n",
       "3    沐白  55  89  75  85  56   0\n",
       "4   苏牧待  85  66  80  47  67   0\n",
       "5  山河之志  54  39  75  42  69   0\n",
       "6    萤火  40  39  90  71  93   0\n",
       "7    烟火  81  75  75  56  94   0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个列\n",
    "data[\"生物\"] = 0\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程\n",
       "0    坎坷  72  81  59  80  55\n",
       "1  温柔岁月  79  38  64  46  95\n",
       "2  时空倒流  69  31  93  49  70\n",
       "3    沐白  55  89  75  85  56\n",
       "4   苏牧待  85  66  80  47  67\n",
       "5  山河之志  54  39  75  42  69\n",
       "6    萤火  40  39  90  71  93\n",
       "7    烟火  81  75  75  56  94"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除一个列\n",
    "del data[\"生物\"]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>81</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>38</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>31</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>89</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>66</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>39</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>39</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>75</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   数学  语文\n",
       "0  81  72\n",
       "1  38  79\n",
       "2  31  69\n",
       "3  89  55\n",
       "4  66  85\n",
       "5  39  54\n",
       "6  39  40\n",
       "7  75  81"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检索多个列\n",
    "data.loc[:,[\"数学\",\"语文\"]] # loc属性，按照显示索引来获取元素，对应的便是iloc索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文\n",
       "1  温柔岁月  79\n",
       "2  时空倒流  69\n",
       "3    沐白  55\n",
       "4   苏牧待  85"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 像numpy那样获取元素\n",
    "data.iloc[1:5,:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程\n",
       "1  温柔岁月  79  38  64  46  95\n",
       "2  时空倒流  69  31  93  49  70\n",
       "5  山河之志  54  39  75  42  69\n",
       "6    萤火  40  39  90  71  93"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 子网掩码 \n",
    "data.loc[data[\"数学\"]<60,:] # 数学成绩不合格的学生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程\n",
       "3    沐白  55  89  75  85  56\n",
       "4   苏牧待  85  66  80  47  67\n",
       "5  山河之志  54  39  75  42  69"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 让人困惑的获取列和获取行\n",
    "# 在抛开loc和iloc时，下面的检索多条数据的方式是获取行\n",
    "data[3:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name    object\n",
       "语文       int32\n",
       "数学       int32\n",
       "外文       int32\n",
       "物理       int32\n",
       "编程       int32\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看类型，初始时是自动判定，但是可以修改\n",
    "data.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['name', '语文', '数学', '外文', '物理', '编程'], dtype='object')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列标签\n",
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 维度\n",
    "data.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 6)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 形状\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name 语文 数学 外文 物理 编程 "
     ]
    }
   ],
   "source": [
    "# __iter__() 迭代columns名称\n",
    "for series in data:\n",
    "    print(series,end=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "坎坷 72 81 59 80 55 "
     ]
    }
   ],
   "source": [
    "# 迭代每列 (列名,Series)\n",
    "for c,s in data.items():\n",
    "    print(s[0],end=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "坎坷 温柔岁月 时空倒流 沐白 苏牧待 山河之志 萤火 烟火 "
     ]
    }
   ],
   "source": [
    "# 迭代每行,(index,Series)\n",
    "for i,s in data.iterrows():\n",
    "    print(s[\"name\"],end=\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 应用与分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>84.852814</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>76.811457</td>\n",
       "      <td>89.442719</td>\n",
       "      <td>74.161985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>88.881944</td>\n",
       "      <td>61.644140</td>\n",
       "      <td>80.000000</td>\n",
       "      <td>67.823300</td>\n",
       "      <td>97.467943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>83.066239</td>\n",
       "      <td>55.677644</td>\n",
       "      <td>96.436508</td>\n",
       "      <td>70.000000</td>\n",
       "      <td>83.666003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>74.161985</td>\n",
       "      <td>94.339811</td>\n",
       "      <td>86.602540</td>\n",
       "      <td>92.195445</td>\n",
       "      <td>74.833148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>92.195445</td>\n",
       "      <td>81.240384</td>\n",
       "      <td>89.442719</td>\n",
       "      <td>68.556546</td>\n",
       "      <td>81.853528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>73.484692</td>\n",
       "      <td>62.449980</td>\n",
       "      <td>86.602540</td>\n",
       "      <td>64.807407</td>\n",
       "      <td>83.066239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>63.245553</td>\n",
       "      <td>62.449980</td>\n",
       "      <td>94.868330</td>\n",
       "      <td>84.261498</td>\n",
       "      <td>96.436508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>90.000000</td>\n",
       "      <td>86.602540</td>\n",
       "      <td>86.602540</td>\n",
       "      <td>74.833148</td>\n",
       "      <td>96.953597</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          语文         数学         外文         物理         编程\n",
       "0  84.852814  90.000000  76.811457  89.442719  74.161985\n",
       "1  88.881944  61.644140  80.000000  67.823300  97.467943\n",
       "2  83.066239  55.677644  96.436508  70.000000  83.666003\n",
       "3  74.161985  94.339811  86.602540  92.195445  74.833148\n",
       "4  92.195445  81.240384  89.442719  68.556546  81.853528\n",
       "5  73.484692  62.449980  86.602540  64.807407  83.066239\n",
       "6  63.245553  62.449980  94.868330  84.261498  96.436508\n",
       "7  90.000000  86.602540  86.602540  74.833148  96.953597"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对每个元素做映射\n",
    "def func(x):\n",
    "    return math.sqrt(x) * 10 # 按照这个计算公式，据说可以提高班级的成绩合格率，而且不影响排名\n",
    "data.iloc[:,1:].applymap(func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    347\n",
       "1    322\n",
       "2    312\n",
       "3    360\n",
       "4    345\n",
       "5    279\n",
       "6    333\n",
       "7    381\n",
       "dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 应用于行或列\n",
    "def func(x):\n",
    "    return x[1:].sum() # 求各科成绩的总成绩\n",
    "data.apply(func,axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>sum</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>69.4</td>\n",
       "      <td>347.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>81.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>64.4</td>\n",
       "      <td>322.0</td>\n",
       "      <td>38.0</td>\n",
       "      <td>95.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>62.4</td>\n",
       "      <td>312.0</td>\n",
       "      <td>31.0</td>\n",
       "      <td>93.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>72.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>89.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>69.0</td>\n",
       "      <td>345.0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>85.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>55.8</td>\n",
       "      <td>279.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>75.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>66.6</td>\n",
       "      <td>333.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>93.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>76.2</td>\n",
       "      <td>381.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>94.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   mean    sum   min   max\n",
       "0  69.4  347.0  55.0  81.0\n",
       "1  64.4  322.0  38.0  95.0\n",
       "2  62.4  312.0  31.0  93.0\n",
       "3  72.0  360.0  55.0  89.0\n",
       "4  69.0  345.0  47.0  85.0\n",
       "5  55.8  279.0  39.0  75.0\n",
       "6  66.6  333.0  39.0  93.0\n",
       "7  76.2  381.0  56.0  94.0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对每一行(axis=1 or 'columns')执行agg方法里的函数\n",
    "data.iloc[:,1:].agg([\"mean\",np.sum,\"min\",\"max\"],axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>数学</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>NaN</td>\n",
       "      <td>57.25</td>\n",
       "      <td>66.875</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>NaN</td>\n",
       "      <td>89.00</td>\n",
       "      <td>85.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       name     数学      语文\n",
       "count   8.0    NaN     NaN\n",
       "mean    NaN  57.25  66.875\n",
       "max     NaN  89.00  85.000"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对指定的列执行agg统计\n",
    "data.agg({\"name\":\"count\",\"数学\":[\"mean\",\"max\"],\"语文\":[\"mean\",\"max\"]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex\n",
       "0    坎坷  72  81  59  80  55    0\n",
       "1  温柔岁月  79  38  64  46  95    0\n",
       "2  时空倒流  69  31  93  49  70    1\n",
       "3    沐白  55  89  75  85  56    0\n",
       "4   苏牧待  85  66  80  47  67    0\n",
       "5  山河之志  54  39  75  42  69    1\n",
       "6    萤火  40  39  90  71  93    0\n",
       "7    烟火  81  75  75  56  94    1"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组，然后统计\n",
    "sex = np.random.randint(0,2,size=8)\n",
    "data[\"sex\"] = sex\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">外文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">物理</th>\n",
       "      <th colspan=\"2\" halign=\"left\">编程</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sex</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>66.2</td>\n",
       "      <td>85</td>\n",
       "      <td>62.600000</td>\n",
       "      <td>89</td>\n",
       "      <td>73.6</td>\n",
       "      <td>90</td>\n",
       "      <td>65.8</td>\n",
       "      <td>85</td>\n",
       "      <td>73.200000</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>68.0</td>\n",
       "      <td>81</td>\n",
       "      <td>48.333333</td>\n",
       "      <td>75</td>\n",
       "      <td>81.0</td>\n",
       "      <td>93</td>\n",
       "      <td>49.0</td>\n",
       "      <td>56</td>\n",
       "      <td>77.666667</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       语文             数学        外文        物理             编程    \n",
       "     mean max       mean max  mean max  mean max       mean max\n",
       "sex                                                            \n",
       "0    66.2  85  62.600000  89  73.6  90  65.8  85  73.200000  95\n",
       "1    68.0  81  48.333333  75  81.0  93  49.0  56  77.666667  94"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照性别来统计各科成绩的平均值和最大值\n",
    "data.groupby(\"sex\").agg([\"mean\",\"max\"],axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>93</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>93</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>85</td>\n",
       "      <td>89</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>93</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  外文  物理  编程\n",
       "0  85  89  90  85  95\n",
       "1  85  89  90  85  95\n",
       "2  81  75  93  56  94\n",
       "3  85  89  90  85  95\n",
       "4  85  89  90  85  95\n",
       "5  81  75  93  56  94\n",
       "6  85  89  90  85  95\n",
       "7  81  75  93  56  94"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照性别分组，然后对每组成员分配该组的统计值，因此最终返回的结果的形状与原形状相同\n",
    "data.iloc[:,1:].groupby(\"sex\").transform(\"max\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sex</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>66.2</td>\n",
       "      <td>62.600000</td>\n",
       "      <td>73.6</td>\n",
       "      <td>65.8</td>\n",
       "      <td>73.200000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>68.0</td>\n",
       "      <td>48.333333</td>\n",
       "      <td>81.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>77.666667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       语文         数学    外文    物理         编程\n",
       "sex                                        \n",
       "0    66.2  62.600000  73.6  65.8  73.200000\n",
       "1    68.0  48.333333  81.0  49.0  77.666667"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# groupby常用的参数：\n",
    "# 1. by： 分组的依据，类型可以是mapping,function,label,or list of labels\n",
    "# 2. axis: 分组的沿轴，{0,or,'index',1,or,'columns'}, 默认为0\n",
    "# 3。sort：bool，是否排序，默认为True\n",
    "# 4. dropna：bool,是否忽略包含缺失值的分组，默认为True\n",
    "\n",
    "data.groupby(by=\"sex\").mean()\n",
    "\n",
    "# 分组是过程，应用才是目的，groupby归根结底还是要走到应用上来，不然就没有太大的意义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex  生物\n",
       "0    坎坷  72  81  59  80  55    0 NaN\n",
       "1  温柔岁月  79  38  64  46  95    0 NaN\n",
       "2  时空倒流  69  31  93  49  70    1 NaN\n",
       "3    沐白  55  89  75  85  56    0 NaN\n",
       "4   苏牧待  85  66  80  47  67    0 NaN\n",
       "5  山河之志  54  39  75  42  69    1 NaN\n",
       "6    萤火  40  39  90  71  93    0 NaN\n",
       "7    烟火  81  75  75  56  94    1 NaN"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 假设生物成绩还没有公布\n",
    "data[\"生物\"] = np.nan\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex   生物\n",
       "0    坎坷  72  81  59  80  55    0  0.0\n",
       "1  温柔岁月  79  38  64  46  95    0  0.0\n",
       "2  时空倒流  69  31  93  49  70    1  0.0\n",
       "3    沐白  55  89  75  85  56    0  0.0\n",
       "4   苏牧待  85  66  80  47  67    0  0.0\n",
       "5  山河之志  54  39  75  42  69    1  0.0\n",
       "6    萤火  40  39  90  71  93    0  0.0\n",
       "7    烟火  81  75  75  56  94    1  0.0"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将缺失值填充为0\n",
    "data.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    name     语文     数学     外文     物理     编程    sex    生物\n",
       "0  False  False  False  False  False  False  False  True\n",
       "1  False  False  False  False  False  False  False  True\n",
       "2  False  False  False  False  False  False  False  True\n",
       "3  False  False  False  False  False  False  False  True\n",
       "4  False  False  False  False  False  False  False  True\n",
       "5  False  False  False  False  False  False  False  True\n",
       "6  False  False  False  False  False  False  False  True\n",
       "7  False  False  False  False  False  False  False  True"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断哪些元素是缺失值, 相反的，还有notna()\n",
    "data.isna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex  生物\n",
       "0    坎坷  72  81  59  80  55    0 NaN\n",
       "1  温柔岁月  79  38  64  46  95    0 NaN\n",
       "2  时空倒流  69  31  93  49  70    1 NaN\n",
       "3    沐白  55  89  75  85  56    0 NaN\n",
       "4   苏牧待  85  66  80  47  67    0 NaN\n",
       "5  山河之志  54  39  75  42  69    1 NaN\n",
       "6    萤火  40  39  90  71  93    0 NaN\n",
       "7    烟火  81  75  75  56  94    1 NaN"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除包含缺失值的行\n",
    "data.dropna(thresh=2,axis=\"index\") # 将包含两个以及以上缺失值的行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \n",
      "E:\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "# 苏牧待和山河之志同学的成绩已经公布\n",
    "data[\"生物\"][4] = 87\n",
    "data[\"生物\"][5] = 76"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex    生物\n",
       "4   苏牧待  85  66  80  47  67    0  87.0\n",
       "5  山河之志  54  39  75  42  69    1  76.0"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 只要包含缺失值的行就删除\n",
    "data.dropna(how=\"any\",axis='index')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引相关 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  外文  物理  编程  sex\n",
       "0    坎坷  72  59  80  55    0\n",
       "1  温柔岁月  79  64  46  95    0\n",
       "2  时空倒流  69  93  49  70    1\n",
       "3    沐白  55  75  85  56    0\n",
       "4   苏牧待  85  80  47  67    0\n",
       "5  山河之志  54  75  42  69    1\n",
       "6    萤火  40  90  71  93    0\n",
       "7    烟火  81  75  56  94    1"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除列\n",
    "data.drop([\"数学\",\"生物\"],axis=\"columns\",inplace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "4    False\n",
       "5     True\n",
       "6    False\n",
       "7     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看是否有重复成绩\n",
    "data.duplicated([\"外文\"],keep=\"first\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  sex    生物\n",
       "0    坎坷  72  81  59  80  55    0   NaN\n",
       "1  温柔岁月  79  38  64  46  95    0   NaN\n",
       "2  时空倒流  69  31  93  49  70    1   NaN\n",
       "3    沐白  55  89  75  85  56    0   NaN\n",
       "4   苏牧待  85  66  80  47  67    0  87.0\n",
       "5    萤火  40  39  90  71  93    0   NaN"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除重复\n",
    "data.drop_duplicates([\"外文\"],inplace=False,ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>sex</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>坎坷</th>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>温柔岁月</th>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>时空倒流</th>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>沐白</th>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>苏牧待</th>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>山河之志</th>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>萤火</th>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>烟火</th>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      语文  数学  外文  物理  编程  sex    生物\n",
       "name                               \n",
       "坎坷    72  81  59  80  55    0   NaN\n",
       "温柔岁月  79  38  64  46  95    0   NaN\n",
       "时空倒流  69  31  93  49  70    1   NaN\n",
       "沐白    55  89  75  85  56    0   NaN\n",
       "苏牧待   85  66  80  47  67    0  87.0\n",
       "山河之志  54  39  75  42  69    1  76.0\n",
       "萤火    40  39  90  71  93    0   NaN\n",
       "烟火    81  75  75  56  94    1   NaN"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将某列调整为索引\n",
    "data.set_index([\"name\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>性别</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>1</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  语文  数学  外文  物理  编程  性别    生物\n",
       "0    坎坷  72  81  59  80  55   0   NaN\n",
       "1  温柔岁月  79  38  64  46  95   0   NaN\n",
       "2  时空倒流  69  31  93  49  70   1   NaN\n",
       "3    沐白  55  89  75  85  56   0   NaN\n",
       "4   苏牧待  85  66  80  47  67   0  87.0\n",
       "5  山河之志  54  39  75  42  69   1  76.0\n",
       "6    萤火  40  39  90  71  93   0   NaN\n",
       "7    烟火  81  75  75  56  94   1   NaN"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改索引和列名的名称\n",
    "data.rename(columns={\"sex\":\"性别\"},index=None,inplace=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>76.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  性别  语文  数学  外文  物理  编程    生物\n",
       "0    坎坷   0  72  81  59  80  55   NaN\n",
       "1  温柔岁月   0  79  38  64  46  95   NaN\n",
       "2  时空倒流   1  69  31  93  49  70   NaN\n",
       "3    沐白   0  55  89  75  85  56   NaN\n",
       "4   苏牧待   0  85  66  80  47  67  87.0\n",
       "5  山河之志   1  54  39  75  42  69  76.0\n",
       "6    萤火   0  40  39  90  71  93   NaN\n",
       "7    烟火   1  81  75  75  56  94   NaN"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调整每列的顺序\n",
    "data = data.reindex(columns=[\"name\",\"性别\",\"语文\",\"数学\",\"外文\",\"物理\",\"编程\",\"生物\"])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 形状与排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1, 2, 2, 1, 2])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给学生增加班级信息\n",
    "class_ = np.random.randint(1,3,size=num)\n",
    "class_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "data[\"班级\"] = class_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>生物</th>\n",
       "      <th>班级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>87.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>76.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  性别  语文  数学  外文  物理  编程    生物  班级\n",
       "0    坎坷   0  72  81  59  80  55   NaN   1\n",
       "1  温柔岁月   0  79  38  64  46  95   NaN   1\n",
       "2  时空倒流   1  69  31  93  49  70   NaN   1\n",
       "3    沐白   0  55  89  75  85  56   NaN   1\n",
       "4   苏牧待   0  85  66  80  47  67  87.0   2\n",
       "5  山河之志   1  54  39  75  42  69  76.0   2\n",
       "6    萤火   0  40  39  90  71  93   NaN   1\n",
       "7    烟火   1  81  75  75  56  94   NaN   2"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>班级</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>1</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>2</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>2</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  性别  语文  数学  外文  物理  编程  班级  生物\n",
       "0    坎坷   0  72  81  59  80  55   1  90\n",
       "1  温柔岁月   0  79  38  64  46  95   1  84\n",
       "2  时空倒流   1  69  31  93  49  70   1  91\n",
       "3    沐白   0  55  89  75  85  56   1  87\n",
       "4   苏牧待   0  85  66  80  47  67   2  99\n",
       "5  山河之志   1  54  39  75  42  69   2  74\n",
       "6    萤火   0  40  39  90  71  93   1  64\n",
       "7    烟火   1  81  75  75  56  94   2  73"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生物成绩公布了！不需要用NaN值了\n",
    "data.drop([\"生物\"],axis=\"columns\",inplace=True)\n",
    "data[\"生物\"] = np.random.randint(60,100,size=num)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Index contains duplicate entries, cannot reshape",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-71-cbe09c41d77c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 调整为数据可视化表\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"语文\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"数学\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"外文\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"班级\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"性别\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mpivot\u001b[1;34m(self, index, columns, values)\u001b[0m\n\u001b[0;32m   7791\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpivot\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7792\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 7793\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   7794\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7795\u001b[0m     _shared_docs[\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\pivot.py\u001b[0m in \u001b[0;36mpivot\u001b[1;34m(data, index, columns, values)\u001b[0m\n\u001b[0;32m    515\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    516\u001b[0m             \u001b[0mindexed\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor_sliced\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmultiindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 517\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mindexed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0munstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcolumns_listlike\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    518\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    519\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36munstack\u001b[1;34m(self, level, fill_value)\u001b[0m\n\u001b[0;32m   8323\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0munstack\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8324\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 8325\u001b[1;33m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0munstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   8326\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   8327\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__finalize__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"unstack\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36munstack\u001b[1;34m(obj, level, fill_value)\u001b[0m\n\u001b[0;32m    445\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    446\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mMultiIndex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 447\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0m_unstack_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfill_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    448\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    449\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdropna\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36m_unstack_frame\u001b[1;34m(obj, level, fill_value)\u001b[0m\n\u001b[0;32m    472\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmgr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    473\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 474\u001b[1;33m         \u001b[0munstacker\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_Unstacker\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlevel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconstructor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    475\u001b[0m         return unstacker.get_result(\n\u001b[0;32m    476\u001b[0m             \u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue_columns\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfill_value\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfill_value\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, index, level, constructor)\u001b[0m\n\u001b[0;32m    131\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Unstacked DataFrame is too big, causing int32 overflow\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    132\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 133\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_selectors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    134\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    135\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mcache_readonly\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mE:\\anaconda\\lib\\site-packages\\pandas\\core\\reshape\\reshape.py\u001b[0m in \u001b[0;36m_make_selectors\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    183\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    184\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mmask\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 185\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Index contains duplicate entries, cannot reshape\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    186\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    187\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup_index\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcomp_index\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Index contains duplicate entries, cannot reshape"
     ]
    }
   ],
   "source": [
    "# 调整为数据可视化表\n",
    "# 注意不能包含重复的值，否则无法达到预期\n",
    "data.pivot(values=[\"语文\",\"数学\",\"外文\"],index=[\"班级\"],columns=\"性别\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">外文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>72</td>\n",
       "      <td>93</td>\n",
       "      <td>61.75</td>\n",
       "      <td>31.0</td>\n",
       "      <td>61.5</td>\n",
       "      <td>69.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "      <td>66.00</td>\n",
       "      <td>57.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>67.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    外文         数学          语文      \n",
       "性别   0   1      0     1     0     1\n",
       "班级                                 \n",
       "1   72  93  61.75  31.0  61.5  69.0\n",
       "2   80  75  66.00  57.0  85.0  67.5"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面生成数据透视表的方式，因为可以指定统计方式，允许重复值的存在\n",
    "data.pivot_table(values=[\"语文\",\"数学\",\"外文\"],index=[\"班级\"],columns=\"性别\",aggfunc=\"mean\")\n",
    "\n",
    "# 主要参数：\n",
    "# 1.values ：统计的列，column to aggregate, optional\n",
    "# 2.index ：column, Grouper, array, or list of the previous\n",
    "# 3.columns ：column, Grouper, array, or list of the previous\n",
    "# 4.aggfunc：function, list of functions, dict, default numpy.mean\n",
    "# 5.fill_value：scalar, default None\n",
    "# 6.margins：bool, default False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">外文</th>\n",
       "      <th colspan=\"3\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"3\" halign=\"left\">语文</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>All</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>All</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>班级</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>72.0</td>\n",
       "      <td>93</td>\n",
       "      <td>76.200000</td>\n",
       "      <td>61.75</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>55.60</td>\n",
       "      <td>61.5</td>\n",
       "      <td>69.0</td>\n",
       "      <td>63.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>80.0</td>\n",
       "      <td>75</td>\n",
       "      <td>76.666667</td>\n",
       "      <td>66.00</td>\n",
       "      <td>57.000000</td>\n",
       "      <td>60.00</td>\n",
       "      <td>85.0</td>\n",
       "      <td>67.5</td>\n",
       "      <td>73.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>73.6</td>\n",
       "      <td>81</td>\n",
       "      <td>76.375000</td>\n",
       "      <td>62.60</td>\n",
       "      <td>48.333333</td>\n",
       "      <td>57.25</td>\n",
       "      <td>66.2</td>\n",
       "      <td>68.0</td>\n",
       "      <td>66.875000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       外文                    数学                      语文                 \n",
       "性别      0   1        All      0          1    All     0     1        All\n",
       "班级                                                                      \n",
       "1    72.0  93  76.200000  61.75  31.000000  55.60  61.5  69.0  63.000000\n",
       "2    80.0  75  76.666667  66.00  57.000000  60.00  85.0  67.5  73.333333\n",
       "All  73.6  81  76.375000  62.60  48.333333  57.25  66.2  68.0  66.875000"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.pivot_table(values=[\"语文\",\"数学\",\"外文\"],index=[\"班级\"],columns=\"性别\",aggfunc=\"mean\",margins=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>编程</th>\n",
       "      <th>班级</th>\n",
       "      <th>生物</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>55</td>\n",
       "      <td>1</td>\n",
       "      <td>90</td>\n",
       "      <td>437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>95</td>\n",
       "      <td>1</td>\n",
       "      <td>84</td>\n",
       "      <td>406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>91</td>\n",
       "      <td>403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>67</td>\n",
       "      <td>2</td>\n",
       "      <td>99</td>\n",
       "      <td>444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>69</td>\n",
       "      <td>2</td>\n",
       "      <td>74</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>93</td>\n",
       "      <td>1</td>\n",
       "      <td>64</td>\n",
       "      <td>397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>94</td>\n",
       "      <td>2</td>\n",
       "      <td>73</td>\n",
       "      <td>454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  性别  语文  数学  外文  物理  编程  班级  生物   总分\n",
       "0    坎坷   0  72  81  59  80  55   1  90  437\n",
       "1  温柔岁月   0  79  38  64  46  95   1  84  406\n",
       "2  时空倒流   1  69  31  93  49  70   1  91  403\n",
       "3    沐白   0  55  89  75  85  56   1  87  447\n",
       "4   苏牧待   0  85  66  80  47  67   2  99  444\n",
       "5  山河之志   1  54  39  75  42  69   2  74  353\n",
       "6    萤火   0  40  39  90  71  93   1  64  397\n",
       "7    烟火   1  81  75  75  56  94   2  73  454"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给学生排名\n",
    "\n",
    "\n",
    "# 计算总分\n",
    "data[\"总分\"] = data.loc[:,[\"语文\",\"数学\",\"外文\",\"物理\",\"生物\",\"编程\"]].agg(\"sum\",axis=\"columns\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分\n",
       "0   1    坎坷   0  72  81  59  80  90  55  437\n",
       "1   1  温柔岁月   0  79  38  64  46  84  95  406\n",
       "2   1  时空倒流   1  69  31  93  49  91  70  403\n",
       "3   1    沐白   0  55  89  75  85  87  56  447\n",
       "4   2   苏牧待   0  85  66  80  47  99  67  444\n",
       "5   2  山河之志   1  54  39  75  42  74  69  353\n",
       "6   1    萤火   0  40  39  90  71  64  93  397\n",
       "7   2    烟火   1  81  75  75  56  73  94  454"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data.reindex(columns=[\"班级\",\"name\",\"性别\",\"语文\",\"数学\",\"外文\",\"物理\",\"生物\",\"编程\",\"总分\"])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分\n",
       "0   1    坎坷   0  72  81  59  80  90  55  437\n",
       "1   1  温柔岁月   0  79  38  64  46  84  95  406\n",
       "2   1  时空倒流   1  69  31  93  49  91  70  403\n",
       "3   1    沐白   0  55  89  75  85  87  56  447\n",
       "6   1    萤火   0  40  39  90  71  64  93  397\n",
       "4   2   苏牧待   0  85  66  80  47  99  67  444\n",
       "5   2  山河之志   1  54  39  75  42  74  69  353\n",
       "7   2    烟火   1  81  75  75  56  73  94  454"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给班级排序\n",
    "data.sort_values([\"班级\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分\n",
       "6   1    萤火   0  40  39  90  71  64  93  397\n",
       "2   1  时空倒流   1  69  31  93  49  91  70  403\n",
       "1   1  温柔岁月   0  79  38  64  46  84  95  406\n",
       "0   1    坎坷   0  72  81  59  80  90  55  437\n",
       "3   1    沐白   0  55  89  75  85  87  56  447\n",
       "5   2  山河之志   1  54  39  75  42  74  69  353\n",
       "4   2   苏牧待   0  85  66  80  47  99  67  444\n",
       "7   2    烟火   1  81  75  75  56  73  94  454"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给每个班的同学排序\n",
    "data.sort_values([\"班级\",\"总分\"]) # 成绩应该高的靠前(降序)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "max_class = 2\n",
    "def temporary_class(x):\n",
    "    return max_class - x[\"班级\"] + 1\n",
    "data[\"临时班级\"] = data.apply(temporary_class,axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>临时班级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  临时班级\n",
       "0   1    坎坷   0  72  81  59  80  90  55  437     2\n",
       "1   1  温柔岁月   0  79  38  64  46  84  95  406     2\n",
       "2   1  时空倒流   1  69  31  93  49  91  70  403     2\n",
       "3   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "4   2   苏牧待   0  85  66  80  47  99  67  444     1\n",
       "5   2  山河之志   1  54  39  75  42  74  69  353     1\n",
       "6   1    萤火   0  40  39  90  71  64  93  397     2\n",
       "7   2    烟火   1  81  75  75  56  73  94  454     1"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>临时班级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  临时班级\n",
       "0   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "1   1    坎坷   0  72  81  59  80  90  55  437     2\n",
       "2   1  温柔岁月   0  79  38  64  46  84  95  406     2\n",
       "3   1  时空倒流   1  69  31  93  49  91  70  403     2\n",
       "4   1    萤火   0  40  39  90  71  64  93  397     2\n",
       "5   2    烟火   1  81  75  75  56  73  94  454     1\n",
       "6   2   苏牧待   0  85  66  80  47  99  67  444     1\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     1"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 折腾了一个临时班级出来，现在可以就排序了\n",
    "data = data.sort_values([\"临时班级\",\"总分\"],ascending=False,ignore_index=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 临时班级这一列没有价值了，删除\n",
    "data.drop([\"临时班级\"],inplace=True,axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saved in data1.pkl\n"
     ]
    }
   ],
   "source": [
    "# 保存当前数据\n",
    "import pickle\n",
    "with open(\"data1.pkl\",\"wb\") as f:\n",
    "    pickle.dump(data,f)\n",
    "print(\"Saved in data1.pkl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "0   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "1   1    坎坷   0  72  81  59  80  90  55  437     4\n",
       "2   1  温柔岁月   0  79  38  64  46  84  95  406     5\n",
       "3   1  时空倒流   1  69  31  93  49  91  70  403     6\n",
       "4   1    萤火   0  40  39  90  71  64  93  397     7\n",
       "5   2    烟火   1  81  75  75  56  73  94  454     1\n",
       "6   2   苏牧待   0  85  66  80  47  99  67  444     3\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     8"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给每个学生附加一个年级排名\n",
    "data[\"rank\"] = data[\"总分\"].rank(axis=0, method=\"first\",na_option=\"bottom\",ascending=False).astype(\"int32\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "0   2    烟火   1  81  75  75  56  73  94  454     1\n",
       "1   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "2   2   苏牧待   0  85  66  80  47  99  67  444     3\n",
       "3   1    坎坷   0  72  81  59  80  90  55  437     4\n",
       "4   1  温柔岁月   0  79  38  64  46  84  95  406     5\n",
       "5   1  时空倒流   1  69  31  93  49  91  70  403     6\n",
       "6   1    萤火   0  40  39  90  71  64  93  397     7\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     8"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一个年级的排行榜\n",
    "data.sort_values(\"总分\",ascending=False,ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>56</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>454</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "0   2    烟火   1  81  75  75  56  73  94  454     1\n",
       "1   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "2   2   苏牧待   0  85  66  80  47  99  67  444     3\n",
       "3   1    坎坷   0  72  81  59  80  90  55  437     4\n",
       "4   1  温柔岁月   0  79  38  64  46  84  95  406     5\n",
       "5   1  时空倒流   1  69  31  93  49  91  70  403     6\n",
       "6   1    萤火   0  40  39  90  71  64  93  397     7\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     8"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = data.sort_values(\"总分\",ascending=False,ignore_index=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n",
      "E:\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "data[\"物理\"][0] += 10\n",
    "data[\"总分\"][0] += 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>66</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>464</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "0   2    烟火   1  81  75  75  66  73  94  464     1\n",
       "1   1    沐白   0  55  89  75  85  87  56  447     2\n",
       "2   2   苏牧待   0  85  66  80  47  99  67  444     3\n",
       "3   1    坎坷   0  72  81  59  80  90  55  437     4\n",
       "4   1  温柔岁月   0  79  38  64  46  84  95  406     5\n",
       "5   1  时空倒流   1  69  31  93  49  91  70  403     6\n",
       "6   1    萤火   0  40  39  90  71  64  93  397     7\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     8"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级 name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "1   1   沐白   0  55  89  75  85  87  56  447     2\n",
       "3   1   坎坷   0  72  81  59  80  90  55  437     4\n",
       "6   1   萤火   0  40  39  90  71  64  93  397     7"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 依据物理成绩选取前3位同学的成绩,另外还有nsmallest()\n",
    "data.nlargest(3,\"物理\",keep=\"last\")\n",
    "\n",
    "# keep为all时，超过执行数量时也全部返回"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 认识stack和unstack\n",
    "# df.stack() 把除了0层的column之外的其他层全部转换为index\n",
    "#            通过level参数可以指定要转化的层\n",
    "#            dropna: bool, default True\n",
    "# df.unstack() \n",
    "#             把除了0层的index之外的其他层全部转换为column\n",
    "#            通过level参数可以指定要转化的层\n",
    "#            fill_value: int,str or dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级 name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "2   2  苏牧待   0  85  66  80  47  99  67  444     3\n",
       "3   1   坎坷   0  72  81  59  80  90  55  437     4"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 无重复随机抽取2位同学的成绩来复查\n",
    "data.sample(2,replace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级 name  性别  语文  数学  外文  物理  生物  编程   总分  rank\n",
       "0   1   坎坷   0  72  81  59  80  90  55  437     4\n",
       "1   1   萤火   0  40  39  90  71  64  93  397     7"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机抽取30%的同学的成绩来复查\n",
    "data.sample(frac=0.3,ignore_index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['不贫困', '比较贫困', '比较贫困', '非常贫困', '不贫困', '比较贫困', '不贫困', '比较贫困'],\n",
       "      dtype='<U4')"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 现在有一份额外的信息，这份信息是评定助学金和励志奖学金的依据\n",
    "poverty = [\"不贫困\",\"比较贫困\",\"非常贫困\"]\n",
    "pty = np.random.choice(poverty,num,p=[0.5,0.4,0.1])\n",
    "pty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 4, 3, 4, 4, 5, 3, 5])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "family_num = np.random.randint(3,6,size=num)\n",
    "family_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>家庭情况</th>\n",
       "      <th>家庭人数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>坎坷</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>时空倒流</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>沐白</td>\n",
       "      <td>非常贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>苏牧待</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>山河之志</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>萤火</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>烟火</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     姓名  家庭情况  家庭人数\n",
       "0    坎坷   不贫困     5\n",
       "1  温柔岁月  比较贫困     4\n",
       "2  时空倒流  比较贫困     3\n",
       "3    沐白  非常贫困     4\n",
       "4   苏牧待   不贫困     4\n",
       "5  山河之志  比较贫困     5\n",
       "6    萤火   不贫困     3\n",
       "7    烟火  比较贫困     5"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = pd.DataFrame({\"姓名\":names,\"家庭情况\":pty,\"家庭人数\":family_num})\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果有新同学进来了，可以使用下面的方式给成绩单追加名额\n",
    "# df.append(df2,ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>班级</th>\n",
       "      <th>name</th>\n",
       "      <th>性别</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>外文</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "      <th>编程</th>\n",
       "      <th>总分</th>\n",
       "      <th>rank</th>\n",
       "      <th>姓名</th>\n",
       "      <th>家庭情况</th>\n",
       "      <th>家庭人数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>烟火</td>\n",
       "      <td>1</td>\n",
       "      <td>81</td>\n",
       "      <td>75</td>\n",
       "      <td>75</td>\n",
       "      <td>66</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>464</td>\n",
       "      <td>1</td>\n",
       "      <td>烟火</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>沐白</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>89</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>87</td>\n",
       "      <td>56</td>\n",
       "      <td>447</td>\n",
       "      <td>2</td>\n",
       "      <td>沐白</td>\n",
       "      <td>非常贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>66</td>\n",
       "      <td>80</td>\n",
       "      <td>47</td>\n",
       "      <td>99</td>\n",
       "      <td>67</td>\n",
       "      <td>444</td>\n",
       "      <td>3</td>\n",
       "      <td>苏牧待</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>0</td>\n",
       "      <td>72</td>\n",
       "      <td>81</td>\n",
       "      <td>59</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>55</td>\n",
       "      <td>437</td>\n",
       "      <td>4</td>\n",
       "      <td>坎坷</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>0</td>\n",
       "      <td>79</td>\n",
       "      <td>38</td>\n",
       "      <td>64</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>95</td>\n",
       "      <td>406</td>\n",
       "      <td>5</td>\n",
       "      <td>温柔岁月</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>1</td>\n",
       "      <td>69</td>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>49</td>\n",
       "      <td>91</td>\n",
       "      <td>70</td>\n",
       "      <td>403</td>\n",
       "      <td>6</td>\n",
       "      <td>时空倒流</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>萤火</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>39</td>\n",
       "      <td>90</td>\n",
       "      <td>71</td>\n",
       "      <td>64</td>\n",
       "      <td>93</td>\n",
       "      <td>397</td>\n",
       "      <td>7</td>\n",
       "      <td>萤火</td>\n",
       "      <td>不贫困</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>1</td>\n",
       "      <td>54</td>\n",
       "      <td>39</td>\n",
       "      <td>75</td>\n",
       "      <td>42</td>\n",
       "      <td>74</td>\n",
       "      <td>69</td>\n",
       "      <td>353</td>\n",
       "      <td>8</td>\n",
       "      <td>山河之志</td>\n",
       "      <td>比较贫困</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   班级  name  性别  语文  数学  外文  物理  生物  编程   总分  rank    姓名  家庭情况  家庭人数\n",
       "0   2    烟火   1  81  75  75  66  73  94  464     1    烟火  比较贫困     5\n",
       "1   1    沐白   0  55  89  75  85  87  56  447     2    沐白  非常贫困     4\n",
       "2   2   苏牧待   0  85  66  80  47  99  67  444     3   苏牧待   不贫困     4\n",
       "3   1    坎坷   0  72  81  59  80  90  55  437     4    坎坷   不贫困     5\n",
       "4   1  温柔岁月   0  79  38  64  46  84  95  406     5  温柔岁月  比较贫困     4\n",
       "5   1  时空倒流   1  69  31  93  49  91  70  403     6  时空倒流  比较贫困     3\n",
       "6   1    萤火   0  40  39  90  71  64  93  397     7    萤火   不贫困     3\n",
       "7   2  山河之志   1  54  39  75  42  74  69  353     8  山河之志  比较贫困     5"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 合并成绩单与学生家庭情况调查表\n",
    "data.merge(data2,left_on=\"name\",right_on=\"姓名\",how=\"left\")\n",
    "\n",
    "# 类似于SQL连接\n",
    "# 连接方式：how:{inner,right,left,outer,cross}\n",
    "# 如果二者的合并的键相同可以用on指定，否则用left_on和right_on分别指定\n",
    "# suffixes: list-like,default is (\"_x\",\"_y\")\n",
    "# 左边和右边对象合并键是否是left_index和right_index: bool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 还有一种比较低级的合并方式\n",
    "# df.join(df2,on=None,lsuffix=\"\",rsuffix=\"\",sort=False)\n",
    "# 只能按照索引来合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新数据\n",
    "# df.update(df2,join=\"left\",overwrite=True) # overwrite=True表示覆盖原数据，否则只覆盖缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存数据\n",
    "with open(\"data2.pkl\",\"wb\") as f:\n",
    "    pickle.dump(data,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
